package com.ubi.core.framework.queue;

public abstract class PollingTask<T> implements Runnable {

    private static final int DEFAULT_SLEEP_MILL_SECONDS = 3000;

    private Queue<T> pollingQueue;

    @Override
    public void run() {
        T t = polling();
        if (t == null)
            sleep();
        doExecute(t);
    }

    void sleep() {
        try {
            Thread.currentThread().join(DEFAULT_SLEEP_MILL_SECONDS);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }

    T polling() {
        return pollingQueue.poll();
    }

    public abstract void doExecute(T t);

    public void setPollingQueue(Queue<T> pollingQueue) {
        this.pollingQueue = pollingQueue;
    }

}
